package com.wf.day_0607.test6;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @author mpk
 * @since 2024/6/7 21:24:13
 */
public class FileCopyTest {
    public static void main(String[] args) throws IOException {


        File from = new File("C:\\Users\\mpk16\\Desktop\\javase\\B");
        File to = new File("C:\\Users\\mpk16\\Desktop\\javase\\A");
        copyfile(from, to);


    }

    private static void copyfile(File src, File dest) throws IOException {

        dest.mkdirs();

        File[] files = src.listFiles();


        for (File file : files) {
            //s是文件
            if (file.isFile()) {

                try (
                        BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
                        BufferedOutputStream ot = new BufferedOutputStream(new FileOutputStream(new File(dest, file.getName())));
                ) {
                    byte[] bytes = new byte[1024];
                    int bytesRead;
                    while ((bytesRead = is.read(bytes)) != -1) {
                        ot.write(bytes, 0, bytesRead); // 写入实际读取的字节数
                    }

                }
                //s是文件夹
            } else {

                copyfile(file, new File(dest, file.getName()));
            }

        }
    }
}


